package com.guangzhi.weijianzhi.activity;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.context.WeiJianZhiEntity;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.http.MyAsyncHttpResponseHandler;
import com.guangzhi.weijianzhi.view.LodingFragmentDialog;
import com.loopj.android.http.AsyncHttpResponseHandler;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class StatusSelectActivity extends BaseActivity implements OnClickListener {

	private String phone;
	private String password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.status_select_layout);
		phone = getIntent().getStringExtra("phone");
		password = getIntent().getStringExtra("password");
		initView();
	}

	private void initView() {
		initTitle();
		titleTextV.setText("选择");
		findViewById(R.id.user).setOnClickListener(this);
		findViewById(R.id.company).setOnClickListener(this);
		goBack.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.user://如果是个人
			newLogin();
			break;
		case R.id.company://如果是注册公司，跳转到公司注册界面
		//	startActivity(new Intent(this, cls))
			break;
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
	@SuppressWarnings("static-access")
	private void newLogin() {
		// TODO Auto-generated method stub
		
		
		HttpRequestUtils.doHttpLogin(phone, password, new AsyncHttpResponseHandler() {
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				try {
					JSONObject json = new JSONObject(new String(arg2));
					if(json.optBoolean("status")){
						JSONObject jsonObj = json.optJSONObject("data");
						dataLogin(jsonObj.optString("code"));
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				// TODO Auto-generated method stub
				
			}
		});
		
	
	}
	
	
	/**
	 * 登录成功返回数据
	 */
	@SuppressWarnings("static-access")
	private void dataLogin(String code ) {
		// TODO Auto-generated method stub
		
		HttpRequestUtils.doHttpLoginData(code,new MyAsyncHttpResponseHandler(getSupportFragmentManager(),getResources()) {
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);
				
				try {
					JSONObject json = new JSONObject(new String(arg2));
					System.out.println(json.toString());
					
					WeiJianZhiEntity.id = json.optString("id");
					WeiJianZhiEntity.username= json.optString("username");
					WeiJianZhiEntity.scope = json.optString("scope");
					WeiJianZhiEntity.tel = json.optString("tel");
					WeiJianZhiEntity.token_type = json.optString("token_type");
					WeiJianZhiEntity.expires_in = json.optString("expires_in");
					WeiJianZhiEntity.refresh_token = json.optString("refresh_token");
					WeiJianZhiEntity.access_token = json.optString("access_token");
					
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				LodingFragmentDialog.dismiss(getSupportFragmentManager());
			}
			
		});
	}
	
	
}
